package tripleo.fmsys;

import org.apache.xmlrpc.XmlRpcClient;
import org.apache.xmlrpc.XmlRpcException;

import java.util.concurrent.Future;
import java.util.Vector;
import java.net.MalformedURLException;
import java.io.IOException;

/**
 * Date: Sep 20, 2005
 * Time: 12:58:27 AM
 * <p/>
 * $Id$
 */
class XmlrpcFMResponse implements FMResponse {
	private FMRequest myRequest;

	public XmlrpcFMResponse(FMRequest aFMRequest) {
		myRequest = aFMRequest;
	}

	public Code code() {
		try {
			RSP rsp=get_response();
			if (myRequest.code .equals(Code.OK)) {
				return Code.valueOf("");
			}else throw new IllegalStateException();
		} catch (MalformedURLException e) {
			e.printStackTrace();
			throw new IllegalStateException();
		}
	}

	public CharSequence value() {
		RSP rsp=null;
		try {
			rsp = get_response();
			if (myRequest.code .equals(Code.OK)) {
				return rsp.val;
			}else throw new IllegalStateException();
		} catch (MalformedURLException e) {
			e.printStackTrace();
			throw new IllegalStateException();
		}
	}

	public CharSequence exception() {
		RSP rsp=null;
		try {
			rsp = get_response();
			if (myRequest.code .equals(Code.OK)) {
				return rsp.val;
			}else throw new IllegalStateException();
		} catch (MalformedURLException e) {
			e.printStackTrace();
			throw new IllegalStateException();
		}
	}

	final private Object sendlock=new Object();
	private RSP response;

	private RSP get_response() throws IOException, XmlRpcException {
		if (response == null) {
			RSP result=null;
			synchronized(sendlock) {
//				myRequest.syst;
				XmlRpcClient cl = new XmlRpcClient(myRequest.syst.xmlhost, myRequest.syst.xmlport);
				Vector v=new Vector();
				cl.execute("fmcall", v);
			}
		}
		return response;
	}

	public Object get() {
		return null;
	}

	class RSP {
		public CharSequence val;
	}
//	Future x;
}
